package com.tensquare.article.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.article.pojo.TbArticle;
import com.tensquare.article.service.impl.TbArticleServiceImpl;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/article")
//跨域----@CrossOrigin
public class TbArticleController {

    @Autowired
    private TbArticleServiceImpl tbArticleService;
    @Autowired
    private RedisTemplate redisTemplate;
    
    
    
    @ApiOperation(value = "文章点赞")
    @PutMapping("/thumbup/{articleId}")
    public Result thumbup(@PathVariable String articleId){
        //模拟用户点赞
        String userId="1";
        String key = "thumbup_article_" + userId + "_" + articleId;
        //查询用户点赞信息，根据用户id和文章id
        Object flag = redisTemplate.opsForValue().get(key);
        //判断查询到结果是否为空
        if (flag==null){ //若为空，则没有点过赞。可以点赞
            tbArticleService.thumbup(articleId,userId);
            //点赞成功,在redis保存点赞信息
            redisTemplate.opsForValue().set(key, "1");
            return new Result(true,StatusCode.OK,"文章点赞成功",flag);
        }
        //若不为空，表示用户点过赞，不能重复点赞
        return new Result(false,StatusCode.ERROR,"不能重复点赞",null);
    }
    
    
    
    @ApiOperation(value = "根据文章id和用户ID，建立订阅的关系，保存的是文章作者id和用户id的关系")
    @PostMapping("/subscribe")
    public Result subscribe(@RequestBody Map<String,Object> map){
        //返回状态，如果返回true就是订阅该文件作者，如果返回false就是取消订阅文章作者
       Boolean flag= tbArticleService.subscribe(map.get("articleId").toString(),map.get("userId").toString());
       //Boolean flag= tbArticleService.subscribe(1+"",2+"");
       //判断订阅还是取消订阅
        if (flag==true){
            return new Result(true,StatusCode.OK,"订阅成功",flag);
        }else {
            return new Result(true,StatusCode.OK,"取消订阅",flag);
        }
    }
    
    

    @ApiOperation(value = "测试公共异常")
    @GetMapping("/exception")
    public Result ExceptionTest(){
        int i=1/0;
        return  null;
    }


    /**
     * 若使用实体类作为查询条件，则实体类中每个属性都需要进行反射的方式判断。成本较高，性能较低
     * 改为用集合方式遍历
     */
    @ApiOperation(value = "根据文章条件来查询文章分页列表")
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String, Object> map
    ){
        //根据条件分页查询
        Page<TbArticle> pageDate=tbArticleService.findByPage(map,page,size);
        //封装分页对象
        PageResult<TbArticle> pageResult=new PageResult<>(pageDate.getTotal(),pageDate.getRecords());
        //返回数据
        return new Result(true,StatusCode.OK,"操作成功",pageResult);
    }

    @ApiOperation(value = "根据文章id删除文章")
    @DeleteMapping("/{articleId}")
    public Result deleteById(@PathVariable String articleId){
        int i = tbArticleService.deleteById(articleId);
        return new Result(true,StatusCode.OK,"删除成功",i);
    }


    @ApiOperation(value = "根据文章Id修改文章")
    @PutMapping("/{articleId}")
    public Result updateById(@PathVariable String articleId,@RequestBody TbArticle article){
        System.out.println(articleId);
        //设置id
        article.setId(articleId);
        //执行修改
        boolean b = tbArticleService.updateArticleById(article);

        return new Result(true,StatusCode.OK,"修改文章成功",b);
    }


    @ApiOperation(value = "新增文章")
    @PostMapping()
    public Result save(@RequestBody TbArticle article){
        int i = tbArticleService.saveArticle(article);
        return new Result(true,StatusCode.OK,"新增文章成功",i);
    }

    @ApiOperation(value = "根据文章id查询文章")
    @GetMapping("/{articleId}")
    public Result findById(@PathVariable String articleId){
        TbArticle article = tbArticleService.findById(articleId);
        return new Result(true,StatusCode.OK,"查询成功",article);
    }


    @ApiOperation(value = "查询文章全部列表")
    @GetMapping()
    public Result findAll(){
        List<TbArticle> tbArticleList= tbArticleService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",tbArticleList);
    }

}
